$(function () {
    var listParams = {
        pagenum: 1,
        pagesize: 5,
        cate_id: '',
        state: ''
    }

    getPhoneList()

    getCateList()

    function getPhoneList() {
        axios({
            method: 'GET',
            url: '/my/article/list',
            params: listParams
        }).then(function (res) {
            if (res.data.code !== 0) {
                return layui.layer.msg('获取手机列表失败')
            }
            var list = []

            res.data.data.forEach(function (item) {
                list.push(`
                    <tr>
                        <td>${item.title}</td>
                        <td>${item.cate_name}</td>
                        <td>${dayjs(item.pub_date).format('YYYY-MM-DD HH:mm:ss')}</td>
                        <td>${item.state}</td>
                        <td>
                            <button type="button" class="layui-btn layui-btn-xs" id="btn-edit" data-id="${item.id}">查看</button>
                            <button type="button" class="layui-btn layui-btn-xs layui-btn-danger btn-del" id="btn-del" data-id="${item.id}">下架</button>
                        </td>
                    </tr>
                `)
            })
            renderPage(res.data.total)
            $('tbody').html(list)
        })
    }

    function getCateList() {
        axios({
            method: 'GET',
            url: '/my/cate/list'
        }).then(function (res) {
            if (res.data.code !== 0) {
                return layui.layer.msg('获取分类数据失败')
            }
            var list = []
            res.data.data.forEach(function (item) {
                list.push(`
                    <option value="${item.id}">${item.cate_name}</option>
                `)
            })
            $('#selectCate').append(list)
            layui.form.render('select')
        })
    }

    $('#form-search').on('submit', function (e) {
        e.preventDefault()

        var cate_id = $('#selectCate').val()
        var state = $('#selectState').val()

        listParams.cate_id = cate_id
        listParams.state = state

        getPhoneList()
    })

    function renderPage(total) {
        layui.laypage.render({
            elem: "pagebox",
            count: total,
            limit: listParams.pagesize,
            curr: listParams.pagenum,
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 5, 8, 10, 15],
            jump: function (obj, first) {
                listParams.pagenum = obj.curr
                listParams.pagesize = obj.limit
                if (!first) {
                    getPhoneList()
                }
            }
        })
    }

    $('tbody').on('click', '#btn-del', function () {
        var id = $(this).attr('data-id')
        var len = $('.btn-del').length

        layer.confirm('确定要下架该手机吗?', { icon: 3, title: '提示' }, function (index) {
            axios({
                method: 'DELETE',
                url: '/my/article/info',
                params: { id: id }
            }).then(function (res) {
                if (res.data.code !== 0) {
                    return layui.layer.msg('下架失败，请稍后再试')
                }
                layui.layer.msg('下架成功')
                if (len === 1) {
                    listParams.pagenum = listParams.pagenum === 1 ? 1 : listParams.pagenum - 1

                }
                getPhoneList()
            })
            layer.close(index);
        });
    })

    $('tbody').on('click', '#btn-edit', function () {
        var id = $(this).attr('data-id')
        location.href = `/Phone/Phshow.html?id=${id}`
    })
})